package com.mengdie.shuidi.ui.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.BitmapCallback;
import com.lzy.okgo.callback.StringCallback;
import com.mengdie.shuidi.AppContext;
import com.mengdie.shuidi.OkHeader;
import com.mengdie.shuidi.R;
import com.mengdie.shuidi.manager.MDAccountMgr;
import com.mengdie.shuidi.model.BaseModel;
import com.mengdie.shuidi.utils.ToastUtil;
import com.mengdie.shuidi.utils.common.TimeUtils;

import org.json.JSONObject;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by apple on 16/10/13.
 */

public class ModifyPasswordActivity extends BaseActivity {
    @Bind(R.id.rl_generic_back)
    RelativeLayout mRlGenericBack;
    @Bind(R.id.tv_generic_title)
    TextView mTvGenericTitle;
    @Bind(R.id.et_modify_old)
    EditText mEtModifyOld;
    @Bind(R.id.et_modify_new)
    EditText mEtModifyNew;
    @Bind(R.id.et_modify_agin)
    EditText mEtModifyAgin;
    @Bind(R.id.et_modify_code)
    EditText mEtModifyCode;
    @Bind(R.id.iv_modify_code)
    ImageView mIvModifyCode;
    @Bind(R.id.tv_modify_submit)
    TextView mTvModifySubmit;
    private String newP;
    private String time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_password);
        ButterKnife.bind(this);
        init();
    }

    @Override
    protected void onStart() {
        super.onStart();

        getCode();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
    }

    public void getCode() {
        time = String.valueOf(TimeUtils.getCurTimeMills());

        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHeadToken(this)).post(AppContext.getInstance().getBaseUrl() + "account/verify")
                .tag(this)
                .upJson(mapVerifyId(time))
                .execute(new BitmapCallback() {
                    @Override
                    public void onSuccess(Bitmap bitmap, Call call, Response response) {
                        mIvModifyCode.setImageBitmap(bitmap);
                    }
                });

    }

    public void init() {
        mTvGenericTitle.setText("修改密码");
    }


    @OnClick({R.id.rl_generic_back, R.id.iv_modify_code, R.id.tv_modify_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_generic_back:
                onBackPressed();
                break;
            case R.id.iv_modify_code:
                getCode();
                break;
            case R.id.tv_modify_submit:
                String oldPwd = mEtModifyOld.getText().toString();
                newP = mEtModifyNew.getText().toString();
                String aignP = mEtModifyAgin.getText().toString();
                String verify = mEtModifyCode.getText().toString();
                if (!TextUtils.isEmpty(oldPwd)) {
                    if (!newP.equals(oldPwd)) {
                        if (newP.equals(aignP)) {
                            if (!TextUtils.isEmpty(newP)) {
                                if (!TextUtils.isEmpty(verify)) {
                                    if (newP.length() >= 6 && newP.length() <= 20) {
                                        modifyPwd(oldPwd, newP, verify);
                                    } else {
                                        ToastUtil.showToast("密码长度必须是6~20位");
                                    }
                                } else {
                                    ToastUtil.showToast("验证码不能为空!");
                                }
                            } else {
                                ToastUtil.showToast("新密码不能为空!");
                            }
                        } else {
                            ToastUtil.showToast("两次输入的新密码不一致!");
                        }
                    } else {
                        ToastUtil.showToast("新旧密码不能相同");
                    }
                } else {
                    ToastUtil.showToast("旧密码不能为空!");
                }
                break;
        }
    }

    public void modifyPwd(String oldpass, final String newpass, String verify) {
        HashMap<String, String> map = new HashMap<>();
        map.put("password", oldpass);
        map.put("new_password", newpass);
        map.put("verify", verify);
        map.put("session_id", MDAccountMgr.getInstance().getSessionId());
        JSONObject json = new JSONObject(map);

        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHeadToken(this)).post(AppContext.getInstance().getBaseUrl() + "account/set_pass")
                .tag(this)
                .upJson(json.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        BaseModel baseModel = gson.fromJson(s, BaseModel.class);
                        if (baseModel.getRet().equals("0")) {
                            ToastUtil.showToast(baseModel.getMsg());
                            MDAccountMgr.getInstance().setLoginPwd(newpass);
                            MDAccountMgr.getInstance().writeToCache(AppContext.getInstance());
                            finish();
                        } else {
                            ToastUtil.showToast(baseModel.getMsg());
                            getCode();
                        }


                    }
                });

    }

}
